package com.promise;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.net.URLEncoder;

public class PrometheusClient {

    public static void main(String[] args) throws Exception {
        // 构造Prometheus查询语句
        String query = "sum(rate(node_cpu_seconds_total{mode!=\"idle\"}[1m])) by (instance)";
        String encodedQuery = URLEncoder.encode(query, "UTF-8");

        // 构造Prometheus HTTP API的URL
        String prometheusUrl = "http://101.43.109.202:9090/api/v1/query?query=" + encodedQuery;

        // 创建HttpClient实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 构造HTTP GET请求
            HttpGet httpGet = new HttpGet(prometheusUrl);

            // 发送HTTP请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                // 获取响应实体
                org.apache.http.HttpEntity entity = response.getEntity();

                // 将响应实体转换为字符串
                String responseBody = EntityUtils.toString(entity);

                // 处理响应数据
                System.out.println("Prometheus Response: " + responseBody);
            }
        }
    }
}


